package cn.kailangtu.myapplication.data;

import android.app.Activity;
import android.os.SystemClock;

import cn.kailangtu.serialport.CommonUtils;

public class Address {

    private static Address instance;

    public static Address getInstance() {
        if (instance == null) {
            instance = new Address();
        }
        return instance;
    }

    public final static int can2F4 = 0x2F4;
    public final static int can4F4 = 0x4F4;
    public final static int can5F4 = 0x5F4;
    public final static int can7F4 = 0x7F4;
    public final static int can281 = 0x281;
    public final static int can381 = 0x381;
    public final static int can481 = 0x481;
    public final static int can581 = 0x581;
    public final static int can681 = 0x681;


    public String time;
    public boolean 座位开关;
    public boolean 手刹;
    public boolean 门关;
    public boolean 安全带系;
    public boolean 前进挡位;
    public boolean 后退挡位;
    public boolean 脚刹;
    public boolean 龟速;
    public boolean 右转向灯;
    public boolean 左转向灯;
    public boolean 双闪灯;
    public boolean 示廓灯;
    public boolean 近光灯;
    public boolean 远光灯;
    public boolean 后雾灯;
    public boolean 安全接近;
    public boolean 安全接近向前;
    public boolean 安全接近向后;
    public boolean 真空泵报警;
    public boolean 刹车油液位低;
    public int ThrottleCommand;
    public int 行走控制器错误代码;
    public int 电机转速;
    public int 故障处理标志;
    public boolean 油泵开关;
    public float 车速;
    public int 车档位;
    public float 液压油压力;
    public float 电池仓温度;


    public float 电池组总电压;
    public float 电池组总电流;
    public int 剩余容量;
    public int 放电计时;
    public int 最高单体电压;
    public int 最高单体位置;
    public String 最高单体位置_VIEW = "";
    public int 最低单体电压;
    public int 最低单体位置;
    public String 最低单体位置_VIEW = "";
    public float 最高电芯温度;
    public int 最高温度位置;
    public String 最高温度位置_VIEW = "";
    public float 最低电芯温度;
    public int 最低温度位置;
    public String 最低温度位置_VIEW = "";
    public int 平均电芯温度;
    public int 单体过压;
    public int 单体欠压;
    public int 总电压过压;
    public int 总电压欠压;
    public int 单体压差大;
    public int 放电过流;
    public int 充电过流;
    public int 温度过高;
    public int 温度过低;
    public int 温差过大;
    public int SOC过低;
    public int 绝缘过低;
    public int 高压互锁故障;
    public int 外部通讯故障;
    public int 内部通讯故障;
    public int 动能回收力度;
    public float[] 行驶里程 = new float[5];
    public float[] 小时计 = new float[5];

    public boolean getDataBit(int address, int byteIndex, int bitIndex) {
        return CanData.getInstance().getDataBit(address, byteIndex, bitIndex);
    }

    public int getData(int address, int byteIndex, int bitIndex, String 数据类型) {
        return CanData.getInstance().getData(address, byteIndex, bitIndex, 数据类型);
    }

    public int getDataBit4(int address , int byteIndex,int bitIndex)
    {
        int byteValue = CanData.getInstance().getData(address,byteIndex,byteIndex,"无符号");
        return (byteValue >> bitIndex) & 0xF;
    }



    public void getDdta() {


        座位开关 = getDataBit(can381, 0, 0);
        脚刹 = getDataBit(can381, 0, 1);
        前进挡位 = getDataBit(can381, 0, 2);
        后退挡位 = getDataBit(can381, 0, 3);
        手刹 = getDataBit(can381, 0, 4);
        门关 = getDataBit(can381,2,3);
        安全带系 = getDataBit(can381,2,2);
        龟速 = getDataBit(can381, 0, 5);
        右转向灯 = getDataBit(can381, 0, 6);
        左转向灯 = getDataBit(can381, 0, 7);
        双闪灯 = getDataBit(can381, 1, 0);
        示廓灯 = getDataBit(can381, 1, 1);
        近光灯 = getDataBit(can381, 1, 2);
        远光灯 = getDataBit(can381, 1, 3);
        后雾灯 = getDataBit(can381, 1, 4);
        安全接近 = getDataBit(can381, 1, 5);
        安全接近向前 = getDataBit(can381, 1, 6);
        安全接近向后 = getDataBit(can381, 1, 7);
        真空泵报警 = getDataBit(can381, 2, 0);
        刹车油液位低 = getDataBit(can381, 2, 1);
        动能回收力度 = getData(can681, 0, 1, "无符号");

        油泵开关 = getDataBit(can381, 3, 2);


        车速 = ((float) getData(can381, 4, 2, "无符号")) / 10;
        车档位 = getData(Address.can381, 6, 1, "无符号");
        ThrottleCommand = getData(can281, 0, 1, "无符号");
        行走控制器错误代码 = getData(can281, 1, 1, "无符号");
        电机转速 = getData(can281, 2, 2, "有符号");
        故障处理标志 = getData(can281, 4, 2, "无符号");
        液压油压力 = ((float) getData(can481, 0, 2, "无符号")) / 100;

        电池仓温度 = ((float) getData(can481, 2, 2, "无符号")) / 10;

        //电池组总电压 = getData(can2F4, 0, 2, "无符号") / 10;
        电池组总电压 =  ((float)getData(0x1C60C109, 0, 2, "无符号")) / 10 ;
       // 电池组总电流 = (new Float(getData(can2F4, 2, 2, "有符号") - 4000)) / 10;
        电池组总电流 = ((float) getData(0x1C60C109, 2, 2, "有符号")) / 10 ;

        //电池组总电流 = (getData(can2F4, 2, 2, "有符号") - 000) / 100;
       // 剩余容量 = getData(can2F4, 4, 1, "无符号");
        剩余容量 = getData(0x1C60C109, 4, 1, "无符号");
        放电计时 = getData(can2F4, 6, 2, "无符号");
        //最高单体电压 = getData(can4F4, 0, 2, "无符号");
        最高单体电压 = getData(0x1C61C109, 0, 2, "无符号");
        //最高单体位置 = getData(can4F4, 2, 1, "无符号");
        最高单体位置_VIEW = "S:"+getDataBit4(0x1C61C109, 2, 0)
                + " M:"+getDataBit4(0x1C61C109,2,4)
                +" C:"+getDataBit4(0x1C61C109,3,0);

        //最低单体电压 = getData(can4F4, 3, 2, "无符号");
        最低单体电压 = getData(0x1C61C109, 4, 2, "无符号");
        //最低单体位置 = getData(can4F4, 5, 1, "无符号");
        最低单体位置_VIEW = "S:"+getDataBit4(0x1C61C109, 6, 0)
                + " M:"+getDataBit4(0x1C61C109,6,4)
                +" C:"+getDataBit4(0x1C61C109,7,0);
        //最高电芯温度 = getData(can5F4, 0, 1, "有符号") - 50;
        最高电芯温度 =  ((float)getData(0x1C62C109, 0, 2, "有符号")) / 10;
       // 最高温度位置 = getData(can5F4, 1, 1, "无符号");
        最高温度位置 = getData(0x1C62C109, 2, 2, "无符号");
        最高温度位置_VIEW = "S:"+getDataBit4(0x1C62C109, 2, 0)
                + " M:"+getDataBit4(0x1C62C109,2,4)
                +" C:"+getDataBit4(0x1C62C109,3,0);
       // 最低电芯温度 = getData(can5F4, 2, 1, "有符号") - 50;
        最低电芯温度 =  ((float)getData(0x1C62C109, 4, 2, "有符号") ) / 10;
        //最低温度位置 = getData(can5F4, 3, 1, "无符号");
        最低温度位置 = getData(0x1C62C109, 6, 2, "无符号");
        最低温度位置_VIEW = "S:"+getDataBit4(0x1C62C109, 6, 0)
                + " M:"+getDataBit4(0x1C62C109,6,4)
                +" C:"+getDataBit4(0x1C62C109,7,0);
        //平均电芯温度 = getData(can5F4, 3, 1, "有符号") - 50;
        平均电芯温度 = getData(can5F4, 3, 1, "有符号") - 50;
        int data1 = getData(can7F4, 0, 1, "无符号");
        int data2 = getData(can7F4, 2, 2, "无符号");
        int data3 = getData(can7F4, 4, 2, "无符号");
        int data4 = getData(can7F4, 6, 2, "无符号");
        单体过压 = CommonUtils.byteBitToInt(data1, 0);
        单体欠压 = CommonUtils.byteBitToInt(data1, 2);
        总电压过压 = CommonUtils.byteBitToInt(data1, 4);
        总电压欠压 = CommonUtils.byteBitToInt(data1, 6);
        单体压差大 = CommonUtils.byteBitToInt(data1, 8);
        放电过流 = CommonUtils.byteBitToInt(data1, 10);
        充电过流 = CommonUtils.byteBitToInt(data1, 12);
        温度过高 = CommonUtils.byteBitToInt(data1, 14);
        温度过低 = CommonUtils.byteBitToInt(data2, 0);
        温差过大 = CommonUtils.byteBitToInt(data2, 2);
        SOC过低 = CommonUtils.byteBitToInt(data2, 4);
        绝缘过低 = CommonUtils.byteBitToInt(data2, 6);
        高压互锁故障 = CommonUtils.byteBitToInt(data2, 8);
        外部通讯故障 = CommonUtils.byteBitToInt(data2, 10);
        内部通讯故障 = CommonUtils.byteBitToInt(data2, 12);


    }

    int index = 0;
    public void testAll()
    {
        new Thread(new Runnable() {
            @Override
            public void run() {
                SystemClock.sleep(10000);
                while(true) {

                    byte[] testData = new byte[8];
                    for(int i =0;i< 8;i++)
                    {
                        testData[i] =  (byte)index;
                    }
                    CanData.getInstance().setNewValue(can2F4, testData);
                    CanData.getInstance().setNewValue(can4F4, testData);
                    CanData.getInstance().setNewValue(can5F4, testData);
                    CanData.getInstance().setNewValue(can7F4, testData);
                    CanData.getInstance().setNewValue(can281, testData);
                    CanData.getInstance().setNewValue(can381, testData);
                    CanData.getInstance().setNewValue(can481, testData);
                    CanData.getInstance().setNewValue(can681, testData);

                    SystemClock.sleep(1000);
                    index++;
                    if(index > 255){
                        index = 0;
                    }
                }
            }
        }).start();

    }


}

